//
//  UpdatePassWordViewController.m
//  Rail
//
//  Created by hychen on 2018/2/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "UpdatePassWordViewController.h"
#import "UpdatePwdTableViewCell.h"
#import "LoginViewController.h"

@interface UpdatePassWordViewController ()

@property (nonatomic, weak) UITextField *originPwdTF;

@property (nonatomic, weak) UITextField *destinPwdTF;

@property (nonatomic, weak) UITextField *destinNewPwdTF;

@end

@implementation UpdatePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"修改密码"];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(165);
    }];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"app_btn_n"] forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"app_btn_p"] forState:UIControlStateHighlighted];
    [sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.tableView.mas_bottom).offset(CZH_ScaleHeight(220));
    }];
    
    self.isOpenHeader = NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sureButtonClick:(UIButton *)button
{
    if (!(self.originPwdTF.text.length>0 && self.destinNewPwdTF.text.length>0 && self.destinPwdTF.text.length>0)) {
        Show_Msg_Error(@"请输入密码")
        return;
    }
    
    if ([self.originPwdTF.text hasWhiteSpace]) {
        Show_Msg_Error(@"原始密码含有空格")
        return;
    }else if ([self.destinPwdTF.text hasWhiteSpace]) {
        Show_Msg_Error(@"新设密码含有空格")
        return;
    }else if ([self.destinNewPwdTF.text hasWhiteSpace]) {
        Show_Msg_Error(@"确认密码含有空格")
        return;
    }else if (![self.destinPwdTF.text isEqualToString:self.destinNewPwdTF.text]) {
        Show_Msg_Error(@"新设置密码两次输入不一致")
        return;
    }else if (self.destinPwdTF.text.length < 6) {
        Show_Msg_Error(@"新设密码长度小于6")
        return;
    }else if (self.destinNewPwdTF.text.length < 6) {
        Show_Msg_Error(@"确认密码长度小于6")
        return;
    }
    
    //网络请求
//    NSString *userAccount = [GlobalManage sharedManage].userInfoModel.account;
    //MD5??
    NSString *oldPwd = [self.originPwdTF.text MD5_lib];
    NSString *newPwd = [self.destinNewPwdTF.text MD5_lib];

    [[NetworkTool sharedNetworkTool] requestDataWithParams:@[API_App_User_UpdatePwd,oldPwd,newPwd] authType:AUTH_LOGIN dataType:JSON_ITEM Success:^(id obj) {
        LoginViewController *loginVc = [[LoginViewController alloc] initWithComeFrom:LoginComeFromTypeUpdatePwd];
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:loginVc];
        [UIApplication sharedApplication].delegate.window.rootViewController = nav;
        Show_Msg_Success(@"修改密码成功,请重新登录")
    } Failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UpdatePwdTableViewCell *cell = [UpdatePwdTableViewCell cellWithTableView:tableView class:[UpdatePwdTableViewCell class]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"原始密码";
        self.originPwdTF = cell.textField;
    }
    else if (indexPath.row == 1) {
        cell.titleLabel.text = @"新设密码";
        self.destinPwdTF = cell.textField;
    }
    else if (indexPath.row == 2) {
        cell.titleLabel.text = @"确认密码";
        self.destinNewPwdTF = cell.textField;
    }
    
    return cell;
}

@end
